using System;
using System.Collections.Generic;
using UnityEngine;

namespace EasyPhysic.Collider
{
    /// <summary>
    /// 碰撞盒代理
    /// </summary>
    public abstract  class ColliderWrapper
    {

        /// <summary>
        /// 标记为脏
        /// </summary>
        public bool IsDirty;
        /// <summary>
        /// 分组Id
        /// </summary>
        public int GroupId
        {
            get=>ShapeInfo.GroupId;
            set => ShapeInfo.GroupId = value;
        }
            
            
        /// <summary>
        /// 唯一Id
        /// </summary>
        public int SerialId{ get=>ShapeInfo.SerialId;
            set => ShapeInfo.SerialId = value;
        }

        /// <summary>
        /// 碰撞盒形状
        /// </summary>
        public  ColliderBody ShapeInfo;

        /// <summary>
        /// 目标组
        /// </summary>
        public int[] TargetGroup;
        

        public ColliderType ColliderType
        {
            get { return ShapeInfo.ColliderType; }
            set
            {
                if (ShapeInfo.ColliderType != value)
                {
                    ShapeInfo.ColliderType = value;
                    SetDirty();
                }

            }
        }
        /// <summary>
        /// 是否有效
        /// </summary>
        public bool IsValid
        {
            get { return ShapeInfo.IsValid; }
            set
            {
                if (ShapeInfo.IsValid != value)
                {
                    ShapeInfo.IsValid = value;
                    SetDirty();
                }

            }
        }





        /// <summary>
        /// 触发
        /// </summary>
        /// <param name="targetId"></param>
        public void Trigger(int targetId)
        {
            OnTrigger(targetId);
        }
        /// <summary>
        /// 触发时调用，允许子对象实现该部分功能
        /// </summary>
        /// <param name="targetId"></param>
        public virtual void OnTrigger(int targetId)
        {
           
        }
        

       

        public void OnDrawGizmos(Color color)
        {
           
        }
        
        static Dictionary<Type,Queue<ColliderWrapper>> _cache = new Dictionary<Type, Queue<ColliderWrapper>>();
        private static int serialId=0;
        public static T Creat<T>() where T : ColliderWrapper,new()
        {
            T collider = null;
            var type= typeof(T);
            if (_cache.ContainsKey(type))
            {
                if (_cache[type].Count > 0)
                {
                    collider= (T)_cache[type].Dequeue();
                }
            }

            if (collider == null)
            {
                collider= new T();
            }
            collider.ColliderType=ColliderType.Trigger;
            collider.ShapeInfo.SerialId=collider.SerialId;
            collider.SerialId=serialId++;
            collider.IsValid = true;
            
            return collider;
        }
        /// <summary>
        /// 回收
        /// </summary>
        public virtual void Release()
        {
            IsValid = true;
            
            var type= GetType();
            if (!_cache.ContainsKey(GetType()))
            {
                _cache.Add(type,new Queue<ColliderWrapper>());
            }
            _cache[type].Enqueue(this);
        }

        void SetDirty()
        {
            IsDirty = true;
        }
    }
}